﻿using Factory.WarehouseManagement.NoticeOfArrivals;
using Microsoft.AspNetCore.Mvc;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;

namespace Factory.Web.Controllers.WarehouseManagement.NoticeOfArrivals
{
    /// <summary>
    /// 到货通知控制器
    /// </summary>
    public class NoticeOfArrivalController : Controller
    {
        /// <summary>
        /// 到货通知服务
        /// </summary>
        private readonly INoticeOfArrivalAppService _noticeOfArrivalService;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="noticeOfArrivalService"></param>
        public NoticeOfArrivalController(INoticeOfArrivalAppService noticeOfArrivalService)
        {
            _noticeOfArrivalService = noticeOfArrivalService;
        }


        #region 视图

        /// <summary>
        /// 主页面
        /// </summary>
        /// <returns></returns>
        public IActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 新增页面
        /// </summary>
        /// <returns></returns>
        public IActionResult AddView()
        {
            return View();
        }

        /// <summary>
        /// 新增子页面页面
        /// </summary>
        /// <returns></returns>
        public IActionResult ArriveDetails(string notificationCode)
        {
            ViewBag.notificationCode = notificationCode;
            return View();
        }
        
        #endregion


        #region 接口

        /// <summary>
        /// 新增到货通知
        /// </summary>
        /// <param name="page">页码</param>
        /// <param name="limit">页容量</param>
        /// <param name="NotificationName">通知单名称</param>
        /// <returns>返回对应数据</returns>
        [HttpGet]
        public async Task<JsonResult> Display(string? notificationName, int page=1, int limit=10)
        {
            var noticeOfArrivalList = await _noticeOfArrivalService.PagedNoticeOfArrival(page, limit, notificationName);
            return Json(new { code = 0, count = noticeOfArrivalList.CurrentPage, data = noticeOfArrivalList.Queryable });

        }

        /// <summary>
        /// 新增到货通知
        /// </summary>
        /// <param name="noticeOfArrival">到货通知实体</param>
        /// <returns>返回受影响行数</returns>
        [HttpPost]
        public async Task<int> Add(NoticeOfArrival noticeOfArrival)
        {
            var i = await _noticeOfArrivalService.Add(noticeOfArrival);
            return i;
        }


        /// <summary>
        /// 获取供应商信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public  JsonResult GetVendor()
        {
            var vndorselectDto = _noticeOfArrivalService.GetVendor();
            return Json(new { Data = vndorselectDto });

        }

        /// <summary>
        /// 添加到货通知
        /// </summary>
        /// <returns>返回受影响行数</returns>
        [HttpPost]
        public async Task<JsonResult> AddCreate(NoticeOfArrival noticeOfArrival)
        {
            var i =await _noticeOfArrivalService.AddCreate(noticeOfArrival);
            return Json(new { code=i});
        }


        /// <summary>
        /// 获取到货信息详情
        /// </summary>
        /// <param name="notificationCode"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<JsonResult> ArriveDetail(string notificationCode)
        {
            var notification = await _noticeOfArrivalService.ArriveDetails(notificationCode);
            return Json(new { data = notification });

        }

        /// <summary>
        /// 获取物料信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<JsonResult> MaterielInfoQuery(string notificationCode, int page, int limit)
        {
            var materielInfo = await _noticeOfArrivalService.MaterielInfoQuery(notificationCode, page, limit);
            return Json(new { code = 0, count = materielInfo.CurrentPage, data = materielInfo.Queryable });

        }


        /// <summary>
        /// 添加物料信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<JsonResult> AddMaterielInfo(MaterielInfo materielInfo)
        {
            var i = await _noticeOfArrivalService.AddMaterielInfo(materielInfo);
            return Json(new { code = i });
        }

        #endregion
    }
}
